//
//  MBProgressHUD+MSCustom.m
//  MSJD
//
//  Created by 箭军刘 on 2018/10/10.
//  Copyright © 2018年 xiangbo. All rights reserved.
//

#import "MBProgressHUD+MSCustom.h"

@implementation MBProgressHUD (MSCustom)

+ (void)showDefaultHUDWithText:(NSString *)text toView:(UIView *)toView {
    if (text.length < 1) {
        return;
    }
    if (!toView) {
        toView = [[UIApplication sharedApplication] keyWindow];
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:toView animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.contentColor = [UIColor blackColor];
    hud.detailsLabel.text = text;
    [hud hideAnimated:YES afterDelay:1.5];
}

+ (void)showToastWithMessage:(NSString *)message toView:(UIView *)toView {
    [MBProgressHUD showToastWithMessage:message toView:toView hideDelay:1.5];
}

+ (void)showToastWithMessage:(NSString *)message toView:(UIView *)toView hideDelay:(CGFloat)hideDelay {
    if (!toView) {
        toView = [UIApplication sharedApplication].keyWindow;
    }
    
    if (![NSThread isMainThread]) {
        dispatch_async(dispatch_get_main_queue(),^{
            [MBProgressHUD showToastWithMessage:message toView:toView];
        });
        return;
    }
    
    // 计算文字标签的宽度
    CGRect rect = [message boundingRectWithSize:CGSizeMake(MS_SCREEN_WIDTH, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15]} context:nil];
    CGFloat width = CGRectGetWidth(rect);
    // 计算提示框的大小
    CGFloat space = 13;
    CGFloat hudHeight = 36;
    CGFloat hudWidth = width + 34 * 2 + hudHeight * 2;
    if (hudWidth > MS_SCREEN_WIDTH - 2 * space - hudHeight) {
        hudWidth = MS_SCREEN_WIDTH - 2 * space - hudHeight;
    }
    
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:toView animated:YES];
    // 设置为文本模式
    hud.mode = MBProgressHUDModeText;
    hud.contentColor = [UIColor blackColor];
    hud.label.text = message;
    
    // bezelView 内容区域
    // 设置为透明背景
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    // 设置背景
//    hud.bezelView.color = rgba(0, 0, 0, 0.75);
    // 设置offset
    [hud setOffset:CGPointMake(0, 100)];
    // 设置内容区域的宽高
    CGRect frame = hud.bezelView.frame;
    frame.size = CGSizeMake(hudWidth, hudHeight);
    hud.bezelView.frame = frame;

    //    CGFloat fontHeight = [UIFont systemFontOfSize:15].lineHeight;
    // 设置圆角
    CGFloat height = hudHeight / 2;
    hud.bezelView.layer.borderColor = [UIColor clearColor].CGColor;
    hud.bezelView.layer.borderWidth = 1.0;
    hud.bezelView.layer.cornerRadius = height;
    hud.bezelView.layer.masksToBounds = YES;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // 12秒之后再消失
    [hud hideAnimated:YES afterDelay:hideDelay];
}

+ (MBProgressHUD *)showMessage:(NSString *)message {
    return [self showMessage:message toView:nil];
}

+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view {
    if (view == nil) view = [[UIApplication sharedApplication] keyWindow];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = message;
    hud.contentColor = [UIColor blackColor];
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // YES代表需要蒙版效果
    //    hud.dimBackground = NO;
    return hud;
}

+ (void)hideHUD {
    [self hideHUDForView:nil];
}

+ (void)hideHUDForView:(UIView *)view {
    if (view == nil) view = [[UIApplication sharedApplication] keyWindow];
    [self hideHUDForView:view animated:YES];
}

@end
